//
//  XMDynamicFilterView.m
//  XunMi
//
//  Created by MacBook on 2024/3/6.
//  Copyright © 2024 TieZhu. All rights reserved.
//

#import "XMDynamicFilterView.h"
#import "XMCollectionFlowLayout.h"
#import "XMDynamicModel.h"

@interface XMDynamicFilterView () <UICollectionViewDelegate, UICollectionViewDataSource ,XMCollectionFlowLayoutDataSource>

@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) XMCollectionFlowLayout *layout;
@property (nonatomic, strong) NSMutableArray <XMDynamicFilterModel *> *dataArray;

@end

@implementation XMDynamicFilterView

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        self.dataArray = @[
        [[XMDynamicFilterModel alloc] initWithTitle:@"最新" isSelected:YES type:XMDynamicFilterTypeXin],
        [[XMDynamicFilterModel alloc] initWithTitle:@"关注" isSelected:NO type:XMDynamicFilterTypeFork],
        [[XMDynamicFilterModel alloc] initWithTitle:@"同城" isSelected:NO type:XMDynamicFilterTypeSame],
        ].mutableCopy;
        self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:self.layout];
        self.collectionView.backgroundColor = UIColor.clearColor;
        [self addSubview:_collectionView];
        [_collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(self);
        }];
        
        self.collectionView.delegate = self;
        self.collectionView.dataSource = self;
        [self.collectionView registerClass:XMDynamicFilterCollectionCell.class forCellWithReuseIdentifier:kStringFromClass(XMDynamicFilterCollectionCell)];
    }
    
    return self;
}

#pragma mark - UICollectionViewDelegate
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.dataArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    XMDynamicFilterCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kStringFromClass(XMDynamicFilterCollectionCell) forIndexPath:indexPath];
    cell.model = self.dataArray[indexPath.item];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    [self.dataArray enumerateObjectsUsingBlock:^(XMDynamicFilterModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if (idx == indexPath.item) {
            obj.isSelected = YES;
            if (self.clickBlock) {
                self.clickBlock(obj.type);
            }
        } else {
            obj.isSelected = NO;
        }
    }];
    [self.collectionView reloadData];
}

#pragma mark - XMCollectionFlowLayoutD
// 一行显示N个
- (NSInteger)collectionView:(UICollectionView *)collectionView layout:(XMCollectionFlowLayout *)layout numberOfColumnInSection:(NSInteger)section {
    return self.dataArray.count;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(XMCollectionFlowLayout *)layout itemWidth:(CGFloat)width heightForItemAtIndexPath:(NSIndexPath *)indexPath {
    return 26;
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(XMCollectionFlowLayout *)layout insetForSectionAtIndex:(NSInteger)section {
    return UIEdgeInsetsMake(10, 20, 10, 20);
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(XMCollectionFlowLayout *)layout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
    return 10;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(XMCollectionFlowLayout *)layout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
    return 32;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(XMCollectionFlowLayout *)layout referenceHeightForHeaderInSection:(NSInteger)section {
    return CGFLOAT_MIN;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(XMCollectionFlowLayout *)layout referenceHeightForFooterInSection:(NSInteger)section {
    return CGFLOAT_MIN;
}

- (XMCollectionFlowLayout *)layout {
    if (_layout == nil) {
        _layout = [[XMCollectionFlowLayout alloc] init];
        _layout.dataSource = self;
    }
    return _layout;
}

@end

@implementation XMDynamicFilterModel

- (instancetype)initWithTitle:(NSString *)title isSelected:(BOOL)isSelected type:(XMDynamicFilterType)type {
    if (self = [super init]) {
        self.title = title;
        self.isSelected = isSelected;
        self.type = type;
    }
    return self;
}

@end

@implementation XMDynamicFilterCollectionCell

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = UIColor.clearColor;
        self.typeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        self.typeBtn.userInteractionEnabled = NO;
        kCornerRadius(_typeBtn, 13, 0, UIColor.clearColor);
        [_typeBtn setBackgroundImage:[UIImage imageWithColor:UIColor.whiteColor] forState:UIControlStateNormal];
        [_typeBtn setBackgroundImage:[UIImage imageWithColor:kAppMainColor] forState:UIControlStateSelected];

        [_typeBtn setTitleColor:kHEX(@"#8C8F9D") forState:UIControlStateNormal];
        [_typeBtn setTitleColor:UIColor.whiteColor forState:UIControlStateSelected];
        _typeBtn.titleLabel.font = kUIFont(14);
        [self.contentView addSubview:_typeBtn];
        [self.typeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(self.contentView);
        }];
    }
    return self;
}

- (void)setModel:(XMDynamicFilterModel *)model {
    _model = model;
    _typeBtn.selected = model.isSelected;
    [self.typeBtn setTitle:model.title forState:UIControlStateNormal];
    
}

@end
